package com.gpframework.module.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.module.domain.GpFormaldehydeContrast;
import com.gpframework.module.module.domain.vo.GpFormaldehydeContrastVo;
import com.gpframework.module.module.domain.req.GpFormaldehydeContrastReq;
import com.gpframework.module.module.domain.req.GpFormaldehydeContrastAdd;
import com.gpframework.module.module.domain.req.GpFormaldehydeContrastUpdate;
import com.gpframework.module.module.service.IGpFormaldehydeContrastService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpFormaldehydeContrastController
 * @Author gpframework
 * @Date 2023-12-11
 * @Version 1.0.0
 * @Description 甲醛自测与送检机构数据对比Controller
 */
@Validated
@Api(tags = "甲醛自测与送检机构数据对比")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/formaldehydecontrast" )
public class GpFormaldehydeContrastController extends BaseController {

    private final IGpFormaldehydeContrastService iGpFormaldehydeContrastService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-11
  * @Param gpFormaldehydeContrast
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:list')")
    @GetMapping("/list")
    public TableDataInfo<GpFormaldehydeContrastVo> list(GpFormaldehydeContrastReq gpFormaldehydeContrastReq)
    {
        startPage();
        QueryWrapper<GpFormaldehydeContrast> lqw = new QueryWrapper<GpFormaldehydeContrast>();
        gpFormaldehydeContrastReq.generatorQuery(lqw,true);
        List<GpFormaldehydeContrastVo> list = iGpFormaldehydeContrastService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpFormaldehydeContrast
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:add')" )
    @Log(title = "添加甲醛自测与送检机构数据对比" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpFormaldehydeContrastAdd gpFormaldehydeContrastAdd) {
        GpFormaldehydeContrast gpFormaldehydeContrast = Convert.convert(new TypeReference<GpFormaldehydeContrast>() {}, gpFormaldehydeContrastAdd);
        return toAjax(iGpFormaldehydeContrastService.save(gpFormaldehydeContrast) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpFormaldehydeContrast
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:edit')" )
    @Log(title = "修改甲醛自测与送检机构数据对比" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpFormaldehydeContrastUpdate> gpFormaldehydeContrastUpdateList) {
        List<GpFormaldehydeContrast> convert = Convert.convert(new TypeReference<List<GpFormaldehydeContrast>>() {
        }, gpFormaldehydeContrastUpdateList);
//        GpFormaldehydeContrast gpFormaldehydeContrast = Convert.convert(new TypeReference<GpFormaldehydeContrast>() {}, gpFormaldehydeContrastUpdate);
        return toAjax(iGpFormaldehydeContrastService.updateBatchById(convert) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpFormaldehydeContrast
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:export')" )
    @Log(title = "导出甲醛自测与送检机构数据对比" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpFormaldehydeContrastReq gpFormaldehydeContrastReq) {
        QueryWrapper<GpFormaldehydeContrast> lqw = new QueryWrapper<GpFormaldehydeContrast>();
        gpFormaldehydeContrastReq.generatorQuery(lqw,true);
        List<GpFormaldehydeContrastVo> list = iGpFormaldehydeContrastService.listVo(lqw);
        ExcelUtil<GpFormaldehydeContrastVo> util = new ExcelUtil<GpFormaldehydeContrastVo>(GpFormaldehydeContrastVo. class);
        return util.exportExcel(list, "formaldehydecontrast" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpFormaldehydeContrast
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpFormaldehydeContrastVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpFormaldehydeContrastService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpFormaldehydeContrast
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:remove')" )
    @Log(title = "删除甲醛自测与送检机构数据对比" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpFormaldehydeContrastService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpFormaldehydeContrast
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('module:formaldehydecontrast:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpFormaldehydeContrastUpdate gpFormaldehydeContrastUpdate) {
        if (gpFormaldehydeContrastUpdate == null || gpFormaldehydeContrastUpdate.getId() == null || gpFormaldehydeContrastUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpFormaldehydeContrastUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpFormaldehydeContrastService.copy(gpFormaldehydeContrastUpdate.getId(), gpFormaldehydeContrastUpdate.getCopyNum()) ? 1 : 0);
    }
}
